package com.bba.manager.sku.controller;

import com.bba.manager.common.model.sku.Car;
import com.bba.manager.common.util.ResponseVO;
import com.bba.manager.sku.service.impl.CarServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SYANDZZY
 * @since 2025-08-12
 */
@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CarServiceImpl carService;

    @PostMapping("/decount/{id}/{num}")
    public ResponseVO decount(
            @PathVariable("id") Long id,
            @PathVariable("num") Integer num
    ) {
        Car car = carService.getByCarId(id);
        if (car.getRepertory() < num) {
            return ResponseVO.fail();
        }
        car.setRepertory(car.getRepertory() - num);
        carService.updateById(car);
        return ResponseVO.success();
    }



    @GetMapping("/update")
    public ResponseVO updateCar(@RequestBody Car car) {
        carService.updateById(car);
        return ResponseVO.success();
    }

    @DeleteMapping("/delete")
    public ResponseVO delete(@RequestParam Long id) {
        carService.removeById(id);
        return ResponseVO.success();
    }

    @PostMapping("/add")
    public ResponseVO add(Car car) {
        carService.save(car);
        return ResponseVO.success();
    }

    @GetMapping("/getall")
    public ResponseVO getall() {
        return ResponseVO.success(carService.list());
    }

    @GetMapping("/getByCar")
    public ResponseVO getByCar(@RequestParam Car car) {
        return ResponseVO.success(carService.getCar(car));
    }
}


